<?php
/**
 * Created by PhpStorm.
 * User: jwb
 * Date: 2020/4/24
 * Time: 18:04
 */

namespace App\Imports;

use App\Patent;
use Carbon\Carbon;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\DB;
use Maatwebsite\Excel\Concerns\ToCollection;

class ProjectEstimateImport implements ToCollection
{
    public function collection(Collection $rows)
    {
        $data = [];
        foreach ($rows as $key => $row) {
            if ($key > 2) {
                $res = $this->getData($row);
                $patent = Patent::query()->where('patent_sn', $res['patent_sn'])->first();
                if (!$patent) {
                    $data[] = $res;
                } else {
                    $patent->update($res);
                }
            }
        }
        collect($data)->unique('patent_sn')->chunk(50)->map(function ($item) {
            $data = $item->values()->all();
            DB::table('patents')->insert($data);
        });
    }

    public function getData($row): array
    {
        $patent_sn = strtr($row[0], ['CN' => '', '.' => '']);
        $patent_type_id = substr($patent_sn, 4, 1);
        $assessment = $this->getAssessment($row);
        $score = ($assessment['fl']['score'] + $assessment['js']['score']) * 0.35 + $assessment['jj']['score'] * 0.3;
        return [
            'patent_sn' => $patent_sn,
            'patent_name' => $row[1],
            'ipc' => strtr($row[2], ["\n" => ';']),
            'apply_date' => $row[3],
            'patent_type_id' => $patent_type_id,
            'legal_status' => '有效',
            'score' => round($score, 2),
            'assessment' => json_encode($assessment),
            'state' => 1,
            'created_at' => Carbon::now(),
            'updated_at' => Carbon::now(),
        ];
    }

    protected function getAssessment($row): array
    {
        return [
            'fl' => [
                'data' => [
                    '稳定性' => $row[4] * 100,
                    '专利布局情况' => $row[5] * 6.6,
                    '不可规避性' => $row[6] * 3.33,
                    '依赖性' => $row[7] * 6.6,
                    '利侵权可判性' => $row[8] * 5,
                    '有效性' => $row[9] * 6.6,
                    '专利许可情况' => $row[10] * 20,
                ],
                'score' => $row[4] * ($row[5] + $row[6] + $row[7] + $row[8] + $row[9] + $row[10])
            ],
            'js' => [
                'data' => [
                    '先进性' => $row[11] * 6.6,
                    '行业发展趋势' => $row[12] * 10,
                    '适用范围' => $row[13] * 5,
                    '配套技术依存度' => $row[14] * 6.6,
                    '可替代性' => $row[15] * 5,
                    '成熟度' => $row[16] * 5,
                ],
                'score' => round(($row[11] + $row[12] + $row[13] + $row[14] + $row[15] + $row[16]), 2)
            ],
            'jj' => [
                'data' => [
                    '市场应用情况' => $row[17] * 4,
                    '市场规模' => $row[18] * 5,
                    '市场占有率' => $row[19] * 5,
                    '竞争情况' => $row[20] * 5,
                    '政策适应性' => $row[21] * 6.6,
                ],
                'score' => $row[17] + $row[18] + $row[19] + $row[20] + $row[21]
            ]
        ];
    }
}
